package com.jianyun.jyzs.model;

import android.util.Log;

import com.google.gson.Gson;
import com.jianyun.jyzs.bean.NewSign2Bean;
import com.jianyun.jyzs.bean.Sign2Bean;
import com.jianyun.jyzs.bean.SignStateBean;
import com.jianyun.jyzs.http.Api;
import com.jianyun.jyzs.http.CustomCallback;
import com.jianyun.jyzs.http.RetrofitHelper;
import com.jianyun.jyzs.model.imdoel.INewSign2Model;
import com.jianyun.jyzs.utils.LoginCache;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangshijun on 2018/8/2.
 */

public class NewSign2Model implements INewSign2Model {

    private static NewSign2Model model;
    private final String serviceUrl;

    private NewSign2Model() {
        serviceUrl = LoginCache.getInstance().getSignVersion().getServiceUrl();
    }

    public static NewSign2Model getInstance() {
        if (model == null) {
            model = new NewSign2Model();
        }
        return model;
    }

    @Override
    public void getRecordeByDate(String enterpriseCode, String userId, String date, final OnGetRecordListener onGetRecordListener) {
        Log.i("test", "记录：" + userId);
        HashMap<String, String> map = new HashMap<>();
        map.put("method","GetSignDayList");
        map.put("enterpriseCode",enterpriseCode);
        map.put("userId",userId);
        map.put("signDate",date);
        Api api = RetrofitHelper.builderNewSignHttp().create(Api.class);
        api.getSignDataList3(serviceUrl,map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Log.e("err", "新版获取记录错误");
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onNext(Object o) {
                        Log.i("test","新版打卡："+o.toString());
                        Gson gson = new Gson();
                        String s = gson.toJson(o);
                        NewSign2Bean sign2Bean = gson.fromJson(s, NewSign2Bean.class);
                        if (sign2Bean.getResult()) {
                            onGetRecordListener.onGetSuccess(sign2Bean.getSignDetail());
                        } else {
                            onGetRecordListener.onFailed(sign2Bean.getMessage());
                        }

                    }
                });

    }

    @Override
    public void newSign2(HashMap<String, String> hashMap, final OnNewSignListener onNewSignListener) {
        Log.i("test","参数："+hashMap.toString());
        Api api = RetrofitHelper.builderNewSignHttp().create(Api.class);
        api.newSign2(serviceUrl,hashMap)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Log.e("err", "新版打卡错误");
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onNext(Object o) {
                        Gson gson = new Gson();
                        String s = gson.toJson(o);
                        JSONObject jsonObject = null;
                        try {
                            jsonObject = new JSONObject(s);
                            boolean result = jsonObject.getBoolean("result");
                            if (result){
                                Sign2Bean sign2Bean = gson.fromJson(s, Sign2Bean.class);
                                onNewSignListener.onSuccess(sign2Bean);

                            }else {
                                onNewSignListener.onFailed(jsonObject.getString("message"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        //Log.i("test","打卡："+s.toString());

                    }
                });
    }

    @Override
    public void updateSign2(HashMap<String, String> hashMap, final OnNewSignListener onNewSignListener) {
        Api api = RetrofitHelper.builderNewSignHttp().create(Api.class);
        api.updateSign2(serviceUrl,hashMap)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onNext(Object o) {
                        Gson gson = new Gson();
                        String s = gson.toJson(o);
                        JSONObject jsonObject = null;
                        try {
                            jsonObject = new JSONObject(s);
                            boolean result = jsonObject.getBoolean("result");
                            if (result){
                                Sign2Bean sign2Bean = gson.fromJson(s, Sign2Bean.class);
                                onNewSignListener.onSuccess(sign2Bean);

                            }else {
                                onNewSignListener.onFailed(jsonObject.getString("message"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        //Log.i("test", "打卡：" + s.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Log.e("err", "新版打卡错误");
                    }
                });
    }

    @Override
    public void getUserSignState(String enterpriseCode, String userId, String signType, final OnGetNowStateListener onGetNowStateListener) {
        Api api = RetrofitHelper.builderNewSignHttp().create(Api.class);
        api.getUserSignState(serviceUrl,"GetUserDKState", enterpriseCode, userId,signType)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<SignStateBean>() {
                    @Override
                    protected void onResponse(SignStateBean response) {
                        //Log.i("test","新的："+response.toString());
                        if (response.getResult()) {
                            onGetNowStateListener.onSuccess(response);
                        } else {
                            onGetNowStateListener.onFailed(response.getMessage());
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                        onGetNowStateListener.onFailed(msg);
                    }
                });
    }
}
